package com.xhx.spring.config;

import com.xhx.spring.service.BeanLifeService;
import com.xhx.spring.service.postprocess.MyBeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * @program: xh-learn-record
 * @description:
 * @author: sch
 * @create: 2023-03-03 15:39
 **/
@Configuration
@ComponentScan("com.xhx.spring")
//@EnableAspectJAutoProxy
//@EnableTransactionManagement
public class AppConfig {

//    @Bean(initMethod = "initMethod", destroyMethod = "destroyMethod")
//    public BeanLifeService beanLifeService() {
//        return new BeanLifeService();
//    }

//    @Bean(initMethod = "initMethod")
//    public MyBeanPostProcessor beanPostProcessor() {
//        return new MyBeanPostProcessor();
//    }

    //    @Bean(initMethod = "initMethod", destroyMethod = "destroyMethod")
//    public MyBean myBean() {
//        return new MyBean();
//    }
//
    @Bean
    public MyBeanPostProcessor myBeanPostProcessor() {
        return new MyBeanPostProcessor();
    }


    String url = "jdbc:mysql://localhost:3307/db0";
    String username = "root";
    String password = "root";

    @Bean
    public JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(dataSource());
    }

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource());
        return transactionManager;
    }
}
